﻿#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Drill.TestSupport.TestDomains.SimpleDomain;
using NUnit.Framework;

namespace Drill.TestSupport
{
	[TestFixture]
	public abstract class BaseDrillBitTests
	{
		protected abstract IDependencyResolver GetDependencyResolverInstanceToTest();

		[Test]
		public void CanDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();

			// Assert
			// Nothing to asset. An exception should not be thrown.
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetInstanceAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetInstance(typeof(ITestObjA));

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetInstanceGenericAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetInstance<ITestObjA>();

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetNamedInstanceAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetInstance(typeof(ITestObjB), "MyObjB");

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetNamedInstanceGenericAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetInstance<ITestObjB>("MyObjB");

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetAllInstancesAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetAllInstances(typeof(ITestObjB));

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetAllInstancesGenericAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetAllInstances<ITestObjB>();

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanGetInstance()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			ITestObjA testObjA = dependencyResolver.GetInstance(typeof(ITestObjA)) as ITestObjA;

			// Assert
			Assert.That(testObjA, Is.Not.Null);
			Assert.That(testObjA, Is.AssignableFrom<TestObjA>());
		}

		[Test]
		public void CanGetInstanceGeneric()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			ITestObjA testObjA = dependencyResolver.GetInstance<ITestObjA>();

			// Assert
			Assert.That(testObjA, Is.Not.Null);
			Assert.That(testObjA, Is.AssignableFrom<TestObjA>());
		}

		[Test]
		public void CanGetNamedInstance()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			ITestObjB myObjB = dependencyResolver.GetInstance(typeof(ITestObjB), "MyObjB") as ITestObjB;

			// Assert
			Assert.That(myObjB, Is.Not.Null);
			Assert.That(myObjB, Is.AssignableFrom<TestObjB>());
		}

		[Test]
		public void CanGetNamedInstanceGeneric()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			ITestObjB myObjB = dependencyResolver.GetInstance<ITestObjB>("MyObjB");

			// Assert
			Assert.That(myObjB, Is.Not.Null);
			Assert.That(myObjB, Is.AssignableFrom<TestObjB>());
		}

		[Test]
		public void CanGetAllInstances()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			IEnumerable enumerable = dependencyResolver.GetAllInstances(typeof(ITestObjB));
			List<ITestObjB> list = enumerable.Cast<ITestObjB>().ToList();

			// Assert
			Assert.That(list.Count, Is.EqualTo(2));
		}

		[Test]
		public void CanGetAllInstancesGeneric()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			IEnumerable<ITestObjB> enumerable = dependencyResolver.GetAllInstances<ITestObjB>();
			List<ITestObjB> list = enumerable.ToList();

			// Assert
			Assert.That(list.Count, Is.EqualTo(2));
		}

		[Test]
		public void CanGetInstanceMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				ITestObjA testObjA = dependencyResolver.GetInstance(typeof(ITestObjA)) as ITestObjA;

				// Assert
				Assert.That(testObjA, Is.Not.Null);
				Assert.That(testObjA, Is.AssignableFrom<TestObjA>());
			}
		}

		[Test]
		public void CanGetInstanceGenericMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				ITestObjA testObjA = dependencyResolver.GetInstance<ITestObjA>();

				// Assert
				Assert.That(testObjA, Is.Not.Null);
				Assert.That(testObjA, Is.AssignableFrom<TestObjA>());
			}
		}

		[Test]
		public void CanGetNamedInstanceMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				ITestObjB myObjB = dependencyResolver.GetInstance(typeof(ITestObjB), "MyObjB") as ITestObjB;

				// Assert
				Assert.That(myObjB, Is.Not.Null);
				Assert.That(myObjB, Is.AssignableFrom<TestObjB>());
			}
		}

		[Test]
		public void CanGetNamedInstanceGenericMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				ITestObjB myObjB = dependencyResolver.GetInstance<ITestObjB>("MyObjB");

				// Assert
				Assert.That(myObjB, Is.Not.Null);
				Assert.That(myObjB, Is.AssignableFrom<TestObjB>());
			}
		}

		[Test]
		public void CanGetAllInstancesMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				IEnumerable enumerable = dependencyResolver.GetAllInstances(typeof(ITestObjB));
				List<ITestObjB> list = enumerable.Cast<ITestObjB>().ToList();

				// Assert
				Assert.That(list.Count, Is.EqualTo(2));
			}
		}

		[Test]
		public void CanGetAllInstancesGenericMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				IEnumerable<ITestObjB> enumerable = dependencyResolver.GetAllInstances<ITestObjB>();
				List<ITestObjB> list = enumerable.ToList();

				// Assert
				Assert.That(list.Count, Is.EqualTo(2));
			}
		}
	}
}